

#ifndef ALLOCATIONMEMORYMANAGER_H_
#define ALLOCATIONMEMORYMANAGER_H_

#include "../../Common/Consts.h"
#include <boost/unordered_map.hpp>
#include <set>
#include "../../Packets/BackSubPkt.h"
#include "../../Packets/PubPkt.h"
#include "../../Memory/MemoryWrapper.h"
#include <string>
#include <queue>
#include "MemoryTable.h"


class AllocationMemoryTable : public MemoryTable {

public:

	AllocationMemoryTable(int parNumMaxSlot, MemoryWrapper* parMemory);

	virtual ~AllocationMemoryTable();

	void updateTable(std::string type, int subscriptionCount, float window, bool resettable, int eventCount);

	void resetTable();

	void computeInsertion(PubPkt *pkt, std::string);

	/*std::set<PubPkt*>*/void computeExtraction(BackSubPkt *pkt, std::set<PubPkt*> &resultSet);

	void fixTable();

private:


	typedef struct AllocTableStruct {
		int actualSize;
		int optimalSize;
		int difference;
		bool resettable;
	} AllocTable;

	class ClusterComparison {
		public:
			bool operator() (const std::pair<int, boost::unordered_map<std::string, AllocTable>::iterator>& lhs, const std::pair<int, boost::unordered_map<std::string, AllocTable>::iterator>& rhs) const {
					return (lhs.first > rhs.first);
			}
		};


	typedef struct InformationTableStruct {
		float window;
		int countSubscriptions;
		int countEvents;
		bool resettable;
	} InformationTable;

	boost::unordered_map<std::string, InformationTable> parameterTable;
	boost::unordered_map<std::string, AllocTable> allocationTable;
	boost::unordered_map<std::string, std::priority_queue<std::pair<timestamp_t, key_t>, std::vector<std::pair<timestamp_t, key_t> >, Comparison > > clustersMap;

	boost::unordered_map<std::string, InformationTable> oldParameterTable;
	boost::unordered_map<std::string, AllocTable> oldAllocationTable;
	boost::unordered_map<std::string, std::priority_queue<std::pair<timestamp_t, key_t>, std::vector<std::pair<timestamp_t, key_t> >, Comparison > > oldClustersMap;

	int numMaxSlot;
	int numUsedSlot;
	int keyCounter;
	MemoryWrapper* memory;
	float weightSum;

	Timer *timer;
};


#endif /* ALLOCATIONMEMORYMANAGER_H_ */
